package com.xinbochuang.machine.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.controller.common.FileController;
import com.xinbochuang.machine.admin.domain.Macinout;
import com.xinbochuang.machine.admin.domain.MacinoutAcceptResult;
import com.xinbochuang.machine.admin.domain.MacinoutAssociation;
import com.xinbochuang.machine.admin.mapper.MacinoutAcceptResultMapper;
import com.xinbochuang.machine.admin.service.IMacinoutAcceptResultService;
import com.xinbochuang.machine.admin.service.IMacinoutAssociationService;
import com.xinbochuang.machine.admin.service.IMacinoutService;
import com.xinbochuang.machine.common.domain.LoginUser;
import com.xinbochuang.machine.common.exception.CustomException;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author 黄晓鹏
 * @date 2020-12-05
 */
@Service
public class MacinoutAcceptResultServiceImpl extends ServiceImpl<MacinoutAcceptResultMapper, MacinoutAcceptResult> implements IMacinoutAcceptResultService {

    @Resource
    private IMacinoutService macinoutService;
    @Autowired
    private IMacinoutAssociationService macinoutAssociationService;
    @Resource
    private TokenService tokenService;

    /**
     * 工单验证
     *
     * @param result 验证信息
     * @param files  上传图片
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean verify(MacinoutAcceptResult result, MultipartFile[] files) {

        MacinoutAssociation macinoutAssociation = macinoutAssociationService.getById(result.getMacinoutAssId());
        // 将工单状态修改为 5已验收
        Macinout macInout = new Macinout();
        macInout.setUuid(macinoutAssociation.getMacinoutId());
        macInout.setOrderStatus(5);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String name = loginUser.getUser().getNickname();
        String now = DateUtil.now();
        macInout.setConfirmTime(now);
        macInout.setConfirmByUser(name);
        macinoutService.checkMacInout(macInout);

//        MacinoutCheckRecord checkRecord = new MacinoutCheckRecord();
//        String recorduuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
//        checkRecord.setUuid(recorduuid);
//        checkRecord.setCheckTime(DateUtil.now());
//        checkRecord.setMacinoutId(macInout.getUuid());
//        checkRecord.setState(macInout.getOrderStatus());
//        checkRecord.setCheckUserName(loginUser.getUser().getUsername());
//        checkRecord.setCheckUserId(loginUser.getUser().getUserId());
//        iMacinoutCheckRecordService.save(checkRecord);
        // 上传图片
        StringBuilder sb = new StringBuilder();
        FileController fileController = new FileController();
        for (MultipartFile file : files) {
            AjaxResult ar = fileController.uploadFile(file);
            String code = ar.get("code").toString();
            String path = ar.get("data").toString();
            if (!code.equals(HttpStatus.HTTP_OK + "") || StrUtil.isBlank(path)) {
                throw new CustomException("上传文件错误");
            }
            sb.append(path).append(",");
        }
        String images = sb.toString();
        if (images.endsWith(",")) {
            images = images.substring(0, images.length() - 1);
        }
        result.setImages(images);
        // 保存验证信息
        String userName = SecurityUtils.getUsername();

        if (StrUtil.isBlank(result.getUuid())) {
            result.setUuid(IdUtil.simpleUUID());
            result.setCreateBy(userName);
            result.setCreateTime(now);
        } else {
            result.setUpdateBy(userName);
            result.setUpdateTime(now);
        }
        return saveOrUpdate(result);
    }
}
